package me.firstapp.api.bbs;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.bbs.Section;
import me.firstapp.service.bbs.SectionService;

@Controller
@RequestMapping(value = "/section")
@Api(value = "/section", description = "话题板块相关接口")
public class SectionApi extends BaseApi {

	@Autowired
	private SectionService sectionService;

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@RequestMapping(value = "/addSection.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增板块", notes = "新增板块")
	public void addSection(String name, String description, Integer sort, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Section> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Section section = sectionService.addSection(name, description, sort, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(section);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findSectionById.do", method = RequestMethod.GET)
	@ApiOperation(value = "通过id查询板块", notes = "通过id查询板块")
	public void findSectionById(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Section> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Section section = sectionService.findById(id, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(section);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateSection.do", method = RequestMethod.POST)
	@ApiOperation(value = "修改板块", notes = "修改板块")
	public void updateSection(Long id, String name, String description, Integer sort, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Section> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Section section = sectionService.updateSection(id, name, description, sort, null, null, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(section);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteSections.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除板块", notes = "删除板块")
	public void deleteSections(String sectionIds, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			sectionService.deleteSections(sectionIds, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getAllSections.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取所有板块", notes = "获取所有板块")
	public void getAllSections(HttpServletRequest request, HttpServletResponse response) {
		ListObject<Section> listResult = new ListObject<Section>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<Section> sections = (List<Section>) redisTemplate.opsForValue().get("sectionsCache" + apiClientId);
			if (sections == null || sections.isEmpty()) {
				sections = sectionService.findAll(apiClientId);
			}
			listResult.setItems(sections);
			listResult.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			listResult.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			listResult.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, listResult);
	}

	@RequestMapping(value = "findSectionPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询板块", notes = "分页查询板块")
	public void findSectionPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Section> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Section> sectionPage = sectionService.findSectionPage(pageNo, pageSize, searchParams, sortParam,
					sortType, apiClientId);
			JsonPage<Section> jsonPage = new JsonPage<Section>();
			jsonPage.setList(sectionPage.getEntities());
			jsonPage.setPageNo(sectionPage.getPageNo());
			jsonPage.setPageSize(sectionPage.getPageSize());
			jsonPage.setTotalCount(sectionPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateSectionCache.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新板块缓存", notes = "更新板块缓存")
	public void updateSectionCache(HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<Section> sections = sectionService.findAll(apiClientId);
			redisTemplate.opsForValue().set("sectionsCache" + apiClientId, sections);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
